package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbutil.DBUtil;

/**
 * Servlet implementation class DeleteMessagesServlet
 */
@WebServlet("/DeleteMessagesServlet")
public class DeleteMessagesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteMessagesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
  		request.setCharacterEncoding("UTF-8");
  		String title = request.getParameter("title");
//  		HttpSession httpSession=request.getSession();
  		DBUtil db=new DBUtil();
		Connection conn = db.getConn();
		try {
			String sql;
			sql="DELETE FROM newsdetail WHERE title = '"+title+"'";
			PreparedStatement ps = conn.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 0) {
				System.out.println("删除成功");
				response.getWriter().println("<script>alert(\"删除成功\");window.location.href=\"./ADMIN/Admin.jsp\"</script>");
			}else {
				System.out.println("失败");
				response.getWriter().println("<script>alert(\"删除失败\");window.location.href=\"./ADMIN/Admin.jsp\"</script>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(db!=null)
				db.release();//关闭数据库
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
